package cn.yanxiaohui.service.web.controller;

import cn.yanxiaohui.common.vo.ActionResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义全局异常处理
 * @author yanxh
 */
@ControllerAdvice(basePackages = "cn.yanxiaohui.service.web.controller")
public class CustomExceptionHandler {

    public final static String ERROE_VIEW = "common/error";

    /**
     * 捕获全局异常，返回error页面
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView myErrorHandler(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(ERROE_VIEW);
        StringBuffer errMsg = new StringBuffer();
        String eType = e.getClass().getName();
        String eMessage = e.getMessage();
        e.printStackTrace();
        errMsg.append("请求出现异常，异常类型：【").append(eType).append("】，异常信息：【").append(eMessage).append("】");
        modelAndView.addObject("errMsg", errMsg.toString());
        return modelAndView;
    }
}
